package com.yaha.controler.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * <功能描述>
 * 
 * @author Frewen.W
 * @version [版本号, 2015-3-25]
 * @since [产品/模块版本]
 */
public class SharedPreferenceUtil {

	public static final String PREFERENCES_NAME = "Common_SharedPreference";

	/**
	 * put long preferences 把新版本的下载ID添加到Preference信息中
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putLong(Context context, String key, long value) {
		SharedPreferences settings = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putLong(key, value);
		editor.commit();
	}

	/**
	 * getLongPreferences 返回一个context内的Preference中对应Key的内容
	 * getSharedPreferences参数为需要获取的Preference的名字，
	 * 第二个为操作的模式，代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static long getLong(Context context, String key) {
		// SharedPreferences 可以用来进行数据的共享，包括应用程序之间，或者同一个应用程序中的不同组件。
		SharedPreferences settings = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		return settings.getLong(key, -1);
	}

	public static void removeData(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.remove(key);
		editor.commit();
	}

	public static int getInt(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		return settings.getInt(key, -1);
	}

	public static void putInt(Context context, String key, int value) {
		SharedPreferences settings = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	public static String getString(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		return settings.getString(key, null);
	}

	public static void putString(Context context, String key, String value) {
		SharedPreferences settings = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();// 需要编辑SharedPreferences的内容时需要调用editor
		editor.putString(key, value);
		editor.commit();
	}
}
